"""
Case Type   : Timescaledb功能测试
Case Name   : show_chunks异常场景验证,合理报错
Create At   : 2025/05/15
Owner       : lihongji
Description :
              1.show_chunks使用字符串
              2.show_chunks使用now()函数
              3.older_than和newer_than无有效的重叠范围
              4.show_chunks调用的表不存在
              5.清理环境
Expect      :
              1.合理报错
              2.合理报错
              3.合理报错
              4.合理报错
              5.清理成功
History     :
"""

import unittest
import os

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node


class Timescaledb(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.constant = Constant()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.Constant = Constant()
        self.primary_sh = CommonSH('PrimaryDbPGmode')
        self.db_user_node = Node(node='PrimaryDbPGmode')
        self.t_name_1 = 'timescaledb_chunkutils_case0009'
        status = self.primary_sh.get_db_cluster_status()
        self.assertTrue("Normal" in status or "Degraded" in status)
        select_extension_cmd = f"SELECT * FROM pg_extension;"
        select_extension_result = self.primary_sh.execut_db_sql(
            select_extension_cmd)
        if "timescaledb" not in select_extension_result:
            create_extension_cmd = f"create extension timescaledb;"
            create_extension_result = (
                self.primary_sh.execut_db_sql(create_extension_cmd))
            self.assertIn(self.constant.extension_create_success,
                          create_extension_result, '创建timescaledb扩展失败')

    def test_add_dimension(self):
        text = '----step1:show_chunks使用字符串 expect:合理报错----'
        self.log.info(text)
        show_chunks = (f"DROP TABLE IF EXISTS PUBLIC.{self.t_name_1};"
                       f"CREATE TABLE PUBLIC.{self.t_name_1}("
                       "time bigint, temp float8, device_id text);"
                       f"SELECT create_hypertable('public.{self.t_name_1}', "
                       "'time', chunk_time_interval => 1, "
                       "create_default_indexes=>FALSE);"
                       f"INSERT INTO PUBLIC.{self.t_name_1} "
                       "VALUES(6, 6.0, 'dev7');"
                       f"SELECT show_chunks('{self.t_name_1}', 'haha');")
        show_chunks_result = self.primary_sh.execut_db_sql(show_chunks)
        self.log.info(show_chunks)
        self.log.info(show_chunks_result)
        self.assertEqual(1, show_chunks_result.count('ERROR'), '执行失败' + text)

        text = '----step2:show_chunks使用now()函数 expect:合理报错----'
        self.log.info(text)
        show_chunks = f"SELECT show_chunks('{self.t_name_1}', now());"
        show_chunks_result = self.primary_sh.execut_db_sql(show_chunks)
        self.log.info(show_chunks)
        self.log.info(show_chunks_result)
        self.assertEqual(1, show_chunks_result.count('ERROR'),
                         '执行失败' + text)

        text = '---step3:older_than和newer_than无有效的重叠范围 expect:合理报错---'
        self.log.info(text)
        show_chunks = (f"SELECT show_chunks('{self.t_name_1}', "
                       "older_than=>3, newer_than=>4);")
        show_chunks_result = self.primary_sh.execut_db_sql(show_chunks)
        self.log.info(show_chunks)
        self.log.info(show_chunks_result)
        self.assertEqual(1, show_chunks_result.count('ERROR'),
                         '执行失败' + text)

        text = '---step4:show_chunks调用的表不存在 expect:合理报错---'
        self.log.info(text)
        show_chunks = (f"SELECT show_chunks('drop_chunk_test4', 5);"
                       f"SELECT show_chunks('drop_chunk_test4');")
        show_chunks_result = self.primary_sh.execut_db_sql(show_chunks)
        self.log.info(show_chunks)
        self.log.info(show_chunks_result)
        self.assertEqual(2, show_chunks_result.count('ERROR'),
                         '执行失败' + text)

    def tearDown(self):
        text = '-----step5:清理环境 expect:成功-----'
        self.log.info(text)
        drop_table = f"drop table PUBLIC.{self.t_name_1}"
        drop_table_result = self.primary_sh.execut_db_sql(drop_table)
        self.assertEqual(self.constant.DROP_TABLE_SUCCESS, drop_table_result,
                         '执行失败' + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
